package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.ITravelContractRefundDao;
import com.eling.elcms.travel.model.ReturnCredentials;
import com.eling.elcms.travel.model.TravelContract;
import com.eling.elcms.travel.model.TravelContractRefund;
import com.eling.elcms.travel.model.view.TravelContractRefundView;
import com.eling.elcms.travel.service.IReturnCredentialsManager;
import com.eling.elcms.travel.service.ITravelContractManager;
import com.eling.elcms.travel.service.ITravelContractRefundManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class TravelContractRefundManagerImpl extends GenericManagerImpl<TravelContractRefund, Long> 
			implements ITravelContractRefundManager{

	ITravelContractRefundDao travelContractRefundDao;
	@Autowired
	private IReturnCredentialsManager returnCredentialsManager;
	@Autowired
	private ITravelContractManager travelContractManager;
	
	@Autowired
	public void setDao(ITravelContractRefundDao dao) {
		this.dao = dao;
		this.travelContractRefundDao = dao;
	}

	@Override
	public List<TravelContractRefundView> queryRefund(TravelContractRefund cond) {
		PropertyUtils.setProperty(cond, "fetchProperties",null);
		List<TravelContractRefund> list = query(cond);
		return Manage(list);
	}

	private List<TravelContractRefundView> Manage(List<TravelContractRefund> list) {
		List<Long> pkList = new ArrayList<Long>();
		for(TravelContractRefund refund : list){
			pkList.add(refund.getTravelContract().getPkTravelContract());
		}
		//根据合同签约pk集合去查询退证
		ReturnCredentials returnCredentials = new ReturnCredentials();
		PropertyUtils.setProperty(returnCredentials, "travelContract.pkTravelContractIn", pkList);
		List<ReturnCredentials> returnList = returnCredentialsManager.query(returnCredentials);
		List<TravelContractRefundView> views = new ArrayList<TravelContractRefundView>();
		for(TravelContractRefund t : list){
			for(ReturnCredentials r : returnList){
				if(t.getTravelContract().getPkTravelContract().equals(r.getTravelContract().getPkTravelContract())){
					TravelContractRefundView view = new TravelContractRefundView();
					view.setTravelContractRefund(t);
					view.setReturnCredentials(r);
					views.add(view);
				}
			}
		}
		return views;
	}
	
	@Override
	public TravelContractRefund saveRefund(TravelContractRefund cond) {
		TravelContractRefund refund = get(cond.getPkTravelContractRefund());
		TravelContract travelContract = travelContractManager.get(cond.getTravelContract().getPkTravelContract());
		travelContract.setStatus(TravelContract.Status.Finish);
		travelContract = travelContractManager.save(travelContract);
		refund.setOperator(cond.getOperator());
		refund.setPrice(cond.getPrice());
		refund.setRefundDate(cond.getRefundDate());
		refund.setStatus(TravelContractRefund.RefundStatus.Refund);
		refund.setTravelContract(travelContract);
		return save(refund);
	}

	@Override
	public List<TravelContractRefundView> searchRefund(SearchCondition sc) {
		PropertyUtils.setProperty(sc, "fetchProperties",null);
		PropertyUtils.setProperty(sc, "queryCondition.fetchProperties",null);
		List<TravelContractRefund> list = search(sc);
		return Manage(list);
	}
}
